package com.example.android.ouactivity.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtils {
	
	public static void getNewsJSON(final String url, final Handler handler){
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				HttpURLConnection conn;
				InputStream is;
				try {
					conn = (HttpURLConnection) new URL(url).openConnection();
					conn.setRequestMethod("GET");
					is = conn.getInputStream();
					BufferedReader reader = new BufferedReader(new InputStreamReader(is));
					String line = "";
					StringBuilder result = new StringBuilder();
					while ( (line = reader.readLine()) != null ){
						result.append(line);
					}
					Message msg = new Message();
					msg.obj = result.toString();
					handler.sendMessage(msg);
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
		}).start();
	}
	//add by chqing
	public static void getWebJsonByPost(final String url, final Handler handler,final String param){
		new Thread(new Runnable() {
			@Override
			public void run() {
				HttpURLConnection conn;
				InputStream is;
				try {
					//实例化url
					URL url1 = new URL(url);
					//调用URL的openConnection()方法,获取HttpURLConnection对象
					conn = (HttpURLConnection)url1.openConnection();
					// 设置请求方法为post
					conn.setRequestMethod("POST");
					// 设置读取超时为10秒
					conn.setReadTimeout(10000);
					// 设置连接网络超时为10秒
					conn.setConnectTimeout(15000);
					// 设置此方法,允许向服务器输出内容,默认情况下,系统不允许向服务器输出内容,
					conn.setDoOutput(true);
					// post请求的参数(形式：key=value&key=value&...&key=value)
					String data = param;
					// 获得一个输出流,向服务器写数据
					OutputStream outputStream = conn.getOutputStream();
					outputStream.write(data.getBytes());//写入数据
					outputStream.flush();//刷新
					outputStream.close();//关闭，不能再写入数据
					int responseCode = conn.getResponseCode();// 获取Http状态码，调用此方法就不必再使用conn.connect()方法
					if (responseCode==200){
						is = conn.getInputStream();
						BufferedReader reader = new BufferedReader(new InputStreamReader(is));
						String line = "";
						StringBuilder result = new StringBuilder();
						while ( (line = reader.readLine()) != null ){
							result.append(line);
						}
						Message msg = new Message();
						msg.obj = result.toString();
						handler.sendMessage(msg);
					}
				}catch (Exception e){
					e.printStackTrace();
				}
			}
		}).start();
	}
	
	public static void setPicBitmap(final ImageView ivPic, final String pic_url){
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					HttpURLConnection conn = (HttpURLConnection) new URL(pic_url).openConnection();
					conn.connect();
					InputStream is = conn.getInputStream();
					Bitmap bitmap = BitmapFactory.decodeStream(is);
					ivPic.setImageBitmap(bitmap);
					is.close();
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
		}).start();
	}
	
}












